import { un } from '@uni-helper/uni-network'

export const httpInstance = un.create({
  baseUrl: 'https://tongdadianzhao.tykhrs.site',
  timeout: 5000,
})

httpInstance.interceptors.request.use(
  (config) => {
    return config
  },
  (error) => {
    uni.showToast({ title: '请求失败', icon: 'none' })
    return Promise.reject(error)
  },
)

httpInstance.interceptors.response.use(
  (response) => {
    if (!(response.data && (response.data as any).code === 1))
      uni.showToast({ title: (response.data as any).msg, icon: 'none' })

    return response
  },
  (error) => {
    uni.showToast({ title: (error as any).message, icon: 'none' })
    return Promise.reject(error)
  },
)

export const mapHttpInstance = un.create({
  baseUrl: 'https://restapi.amap.com',
  timeout: 10000,
})
